import _ from "@/assets/utils"
import * as TYPES from "../action-types"

const initial = {
    tableData: null,
}
export const taskReducer = function (state = initial, action) {
    state = _.clone(true, state)
    switch (action.type) {
        case TYPES.TASK_LIST:
            state.tableData = action.list
            break
        case TYPES.TASK_UPDATE:
            if (Array.isArray(state.tableData)) {
                console.log(state.tableData,2222)
                state.tableData = state.tableData.map(item => {
                    if (+item.id === +action.id) {
                        item.state = 2
                        item.complete = new Date().toLocaleString('zh-CN', {hour12: false})
                    }
                    return item
                })
            }
            break
        case TYPES.TASK_REMOVE:
            let {tableData} = state
            if (Array.isArray(tableData)) {
                state.tableData = tableData.filter(item => {
                    return +item.id !== +action.id
                })
            }
            break
        default:

    }
    return state
}

// 获取全部任务
// dispatch({type:'TASK_LIST',list:[...]})
// 删除任务
// dispatch({type:'TASK_REMOVE',id:xxx)
// 完成任务
// dispatch({type:'TASK_UPDATE',id:xxx})
